package com.cj.user.service.designpatterns.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.springframework.boot.autoconfigure.cache.CacheProperties;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author cheng jun
 * @Description: 代理模式:cglib动态代理(不需要被代理类实现接口)--------------------AOP
 * @Date 2021/7/14 21:37
 */
public class People3 {

    public void move() {
        System.out.println("走路------------------");
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(People3.class);
        enhancer.setCallback(new TimeHander());
        People3 people3 = (People3) enhancer.create();
        people3.move();

        Enhancer enhancer2 = new Enhancer();
        enhancer2.setSuperclass(People4.class);
        enhancer2.setCallback(new TimeHander());
        People4 people4 = (People4) enhancer2.create();
        people4.move();
    }
}

class TimeHander implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("start------------------");
        Object o1 = methodProxy.invokeSuper(o, objects);
        System.out.println("end--------------------");
        return null;
    }
}
class People4 {
    public void move() {
        System.out.println("跑步--------------------------");
    }
}
